package edu.bw.advice;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class ServiceAdvice {
    public void beforeAdvice(JoinPoint joinPoint){
        System.out.println(Arrays.toString(joinPoint.getArgs()) + " " + joinPoint.getSignature());
        System.out.println("前置通知");
    }
    public void afterReturnAdvice(JoinPoint joinPoint ,Object retVal){
        System.out.println(Arrays.toString(joinPoint.getArgs()) + " " + joinPoint.getSignature());
        System.out.println("获取到的返回值:"+retVal);
        System.out.println("后置通知");
    }
    public void finallyAdvice(){
        System.out.println("最终通知");
    }
    public void afterThrowingAdvice(){
        System.out.println("异常通知");
    }
    public void aroundAdvice(ProceedingJoinPoint pjp){
        System.out.println(Arrays.toString(pjp.getArgs()) + " " + pjp.getSignature());
        System.out.println("环绕通知");
        try {
            System.out.println("前置通知");
            Object retVal=pjp.proceed();
            System.out.println("获取到的返回值:"+retVal);
            System.out.println("后置通知");
        } catch (Throwable e) {
            System.out.println("异常通知");
            throw new RuntimeException(e);
        } finally {
            System.out.println("最终通知");
        }

    }

}
